﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
//using System.Web.Mvc;
using System.Xml;
//using System.Data.EntityModel;
//using LottModel;

/// <summary>
/// Zusammenfassungsbeschreibung für Translate
/// </summary>
public class H
{
    private static string _language;
    private static bool _isGerman;
    public static string Language
    {
        get
        {
            _language = HttpContext.Current.Request.Params["lang"];
            if (_language == null)
                _language = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
            if (_language != "de")
            {
                _language = "en";
                _isGerman = false;
            }
            else
            {
                _language = "de";
                _isGerman = true;
            }
            return _language;
        }
    }
    public static string EnDe(string english, string german)
    {
        return (Language == "de") ? german : english;
    }
    public static string ChangeLanguageHref(string language)
    {
        var current = HttpContext.Current.Request.RawUrl;
        // Remove application path from rawurl
		if (HttpContext.Current.Request.ApplicationPath.Length <= current.Length)
		{
			if (current.Substring(0, HttpContext.Current.Request.ApplicationPath.Length) == HttpContext.Current.Request.ApplicationPath)
				current = current.Substring(HttpContext.Current.Request.ApplicationPath.Length);
		}
        string newPath = current;
        if (current.Length > 3)
        {
            if (current.Substring(0, 4) == "/en/" || current.Substring(0, 4) == "/de/")
                newPath = current.Substring(4);
        }
        if (newPath.Length > 0 && newPath[0] == '/')
            newPath = newPath.Substring(1);
        return VirtualPathUtility.MakeRelative("~" + current, "~/" + language + "/"+ newPath);
    }
    public static string EnglishStyle
    {
        get {
            return (_language == "en") ? "style=\"font-weight:bold\"" : "";
        }
    }
    public static string GermanStyle
    {
        get {
        return (_language == "de") ? "style=\"font-weight:bold\"" : "";
        }
    }
    public static bool IsGerman
    {
        get
        {
            return _isGerman;
        }
    }
    public static bool IsEnglish
    {
        get
        {
            return !_isGerman;
        }
    }
    public static string Link(string url)
    {
        var current = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;
        return VirtualPathUtility.MakeRelative(current, "~/" + url);
    }
    public static string Href(string url)
    {
        return HttpContext.Current.Request.ApplicationPath + "/" + url;
    }
    private static void RedirectToNewTranslation( string english, string language)
    {
        HttpContext context = HttpContext.Current;
        string redirect = "~/Admin/NewTranslation.cshtml?"
            + "english=" + context.Server.UrlEncode(english)
            + "&language=" + context.Server.UrlEncode(language)
            + "&page=" + context.Server.UrlEncode(context.Request.Url.LocalPath);
        context.Response.Redirect(redirect);
    }
    public static string RootHref(string path)
    {
        return "http://www.muprotect.com/" + path;
    }

//	public static void Transfer(string url)
//	{
		// Create URI builder
		//var Request = HttpContext.Current.Request;
		//var Server = HttpContext.Current.Server;
		//var uriBuilder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, Request.ApplicationPath);
		//// Add destination URI
		//uriBuilder.Path += url;
		//// Because UriBuilder escapes URI decode before passing as an argument
		//string path = Server.UrlDecode(uriBuilder.Uri.PathAndQuery);
		//// Rewrite path
		//HttpContext.Current.RewritePath(path, false);
		//IHttpHandler httpHandler = new MvcHttpHandler();
		//// Process request
		//httpHandler.ProcessRequest(HttpContext.Current);
//	}

	public static void Log(string line)
	{
		string lineToWrite = DateTime.UtcNow.ToShortDateString() + " " + DateTime.UtcNow.ToShortTimeString() + " " + line + "\r\n";
		bool isWritten = false;
		for (int i = 0; (i < 3) && !isWritten; ++i)
		{
			try
			{
				System.IO.File.AppendAllText(HttpContext.Current.Server.MapPath("~/App_Data/log.txt"), lineToWrite);
				isWritten = true;
			}
			catch (Exception)
			{
				System.Threading.Thread.Sleep(300);
			}
		}
	}

	public static void LogAccess()
	{
		var request = HttpContext.Current.Request;
		var server = HttpContext.Current.Server;

		string remoteIP = IP.RemoteIp;
		var friendOrFoe = IP.FriendOrFoeStatus;
		if( (friendOrFoe == IP.FriendOrFoe.Foe)	&& !request.Path.Contains("/Transfers/") )
		{
			var response = HttpContext.Current.Response;
			Mail.UnauthorizedAccess();
			response.Redirect("Transfers/license.cshtml");
		}
		// Log all unknowns or foes:
		if( friendOrFoe != IP.FriendOrFoe.Friend )
			H.Log(remoteIP + " accessed www.jordanstevens.com" + HttpContext.Current.Request.Path + " using UserAgent=" + HttpContext.Current.Request.UserAgent);
	}

	public static string GetSaying()
	{
		var doc = new XmlDocument();
		doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/Sayings.xml"));
		var nodes = doc.SelectNodes("/sayings/saying");
		var index = DateTime.Now.Ticks % nodes.Count;
		var saying = nodes[(int)index][Language].InnerText;
		return saying;
	}

	//public static void Transfer(string url)
	//{
	//    var Request = HttpContext.Current.Request;
	//    var Server = HttpContext.Current.Server;
	//    // Create URI builder
	//    var uriBuilder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, Request.ApplicationPath);
	//    // Add destination URI
	//    uriBuilder.Path += url;
	//    // Because UriBuilder escapes URI decode before passing as an argument
	//    string path = Server.UrlDecode(uriBuilder.Uri.PathAndQuery);
	//    // Rewrite path
	//    HttpContext.Current.RewritePath(path, false);
	//    IHttpHandler httpHandler = new MvcHttpHandler();
	//    // Process request
	//    httpHandler.ProcessRequest(HttpContext.Current);
	//}

}
